package com.aletto.general_store.presenters;

import android.content.Context;

import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.data.model.PageResponse;
import com.aletto.general_store.data.model.Store;
import com.aletto.general_store.presenters.core.SimplePageTemplate;
import com.aletto.general_store.ui.adapters.StoreGoodsAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.sisyphus.spinach.core.INetPageTask;
import com.sisyphus.spinach.core.model.IPageResponse;
import com.sisyphus.spinach.core.model.IResponse;
import com.sisyphus.spinach.network.presenters.NetPageTask;
import com.sisyphus.spinach.ui.configs.impl.DefaultPageTemplate;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.rxjava3.core.Observable;

public class SearchStorePageTemplate extends SimplePageTemplate<Store> {

    private String keyword;

    public SearchStorePageTemplate(RecyclerView recyclerView, SmartRefreshLayout refreshLayout) {
        super(recyclerView, refreshLayout);
    }

    public SearchStorePageTemplate params(String keyword) {
        this.keyword = keyword;
        return this;
    }

    @Override
    public BaseQuickAdapter<Store, BaseViewHolder> adapter(RecyclerView recyclerView) {
        Context context = recyclerView.getContext();
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        return new StoreGoodsAdapter();
    }

    @Override
    public INetPageTask.Presenter<Store, INetPageTask.View<Store>> presenter() {
        return new NetPageTask<Store>() {
            @Override
            public Observable<? extends IResponse<? extends IPageResponse<Store>>> execute(int page) {
                return DataRepository.getInstance().storeLists(page, PageResponse.SIZE, keyword);
            }
        };
    }

}
